package com.example.sample.activity;

import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.media.MediaPlayer.OnInfoListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.MediaController;
import android.widget.VideoView;

import com.example.app.BaseActivity;
import com.example.sample.R;

public class VideoViewActivity extends BaseActivity {
	
	private static final String TAG = "VideoViewActivity";
	private VideoView mVideoView;
	private VideoView mVideoView2;
	private EditText mEt;
	private View mStart;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_video_view);
		mVideoView = (VideoView)findViewById(R.id.videoView);
		mVideoView2 = (VideoView)findViewById(R.id.videoView2);
		mEt = (EditText)findViewById(R.id.et);
		mStart = findViewById(R.id.start);
		mStart.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				String url = mEt.getText().toString();
				url = "http://play.stream.show.xunlei.com/5E/B4/5EB4C04FC20AF7AE314E25719CB23B67.mp4";
				url = "file:///sdcard/a.mp4";
				play(mVideoView, url);
				play(mVideoView2, url);
			}
		});
		initVideoView(mVideoView);
		initVideoView(mVideoView2);
	}
	
	private void play(VideoView vv, String url){
		Uri uri = Uri.parse(url);
		vv.setVideoURI(uri);
		vv.requestFocus();
		vv.start();
	}

	private void initVideoView(VideoView vv){
		MediaController mc = new MediaController(this);
		
		vv.setMediaController(mc);
		mc.setMediaPlayer(vv);
		
		vv.setOnInfoListener(new OnInfoListener() {
			
			@Override
			public boolean onInfo(MediaPlayer mp, int what, int extra) {
				Log.i(TAG, "onInfo what=" + what + " extra="+extra);
				return false;
			}
		});
		vv.setOnPreparedListener(new OnPreparedListener() {
			
			@Override
			public void onPrepared(MediaPlayer mp) {
				Log.i(TAG, "onPrepared");
			}
		});
		vv.setOnCompletionListener(new OnCompletionListener() {
			
			@Override
			public void onCompletion(MediaPlayer mp) {
				Log.i(TAG, "onCompletion");
			}
		});
		vv.setOnErrorListener(new OnErrorListener() {
			
			@Override
			public boolean onError(MediaPlayer mp, int what, int extra) {
				Log.i(TAG, "onError what=" + what);
				return false;
			}
		});
	}
}
